package com.water.camera.mark;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.App;
import com.water.camera.bean.EditItem;
import com.water.camera.databinding.LayoutWatermark16Binding;
import com.water.camera.util.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Mark16 extends BaseMark {

    LayoutWatermark16Binding binding;

    public Mark16(@NonNull Context context) {
        super(context);
    }

    public Mark16(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Mark16(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(int type) {
        if (type == 0) {
            binding = LayoutWatermark16Binding.inflate(LayoutInflater.from(getContext()));
            addView(binding.getRoot());
        } else {
            binding = LayoutWatermark16Binding.inflate(LayoutInflater.from(getContext()), this, true);
        }
        update();
        if (type == 0) {
            hideView(binding.binding6, binding.binding8, binding.binding10);
        }
    }

    @Override
    public void update() {
        setText(binding.binding1, "time16", binding.time);
        setTime(binding.binding1, new SimpleDateFormat("HH:mm"));
        if (!setTime2(binding.binding3, new SimpleDateFormat("yyyy.MM.dd"))) {
            binding.binding3.setText("2023.03.09 星期四");
        }

        setText(binding.binding5, "didian16", binding.binding4);
        setText(binding.binding7, "jingwei16", binding.binding6);

        setText(binding.binding9, "weather16", binding.binding8);
        setText(binding.binding11, "beizhu16", binding.binding10);

        super.update();
    }

    protected boolean setTime2(TextView tv, SimpleDateFormat format) {
        String key = getTimeKey();
        if (TextUtils.isEmpty(key)) {
            String tmp = getClass().getName().substring("com.water.camera.mark.Mark".length());
            key = "time" + tmp + "_time";
        } else {
            key = key + "_time";
        }
        long time = SPHelper.getInstance(App.getApp()).getLong(key, 0);
        if (time > 0) {
            tv.setText(format.format(time) + " " + Utils.dateToWeek(time));
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<EditItem> getEditItems() {
        ArrayList<EditItem> list = new ArrayList<>();
        list.add(new EditItem("time16", "时间", true));
        list.add(new EditItem("didian16", "地点"));
        list.add(new EditItem("jingwei16", "经纬度"));
        list.add(new EditItem("weather16", "天气"));
        list.add(new EditItem("beizhu16", "备注"));
        return list;
    }

    @Override
    public void onValueChange(EditItem item) {
        if (item.isTime) {
            setTime(binding.binding1, new SimpleDateFormat("HH:mm"));
            setTime2(binding.binding3, new SimpleDateFormat("yyyy.MM.dd"));
        } else {
            super.onValueChange(item);
        }
    }

    @Override
    public void onShowChange(EditItem item) {
        if (item.isTime) {
            if (item.show) {
                binding.time.setVisibility(View.VISIBLE);
                binding.binding2.setVisibility(View.VISIBLE);
            } else {
                binding.time.setVisibility(View.GONE);
                binding.binding2.setVisibility(View.GONE);
            }
        } else {
            super.onShowChange(item);
        }
    }
}
